ENT.Type             = "anim"
ENT.Base             = "base_entity"
ENT.PrintName        = "Webscreen"
ENT.Author			 = "Marcusramse1"
ENT.Contact			= ""
ENT.Purpose			= "WebsiteScreen"
ENT.Instructions	= ""
if SERVER then
	require("datastream")
	AddCSLuaFile("shared.lua")
	function ENT:Initialize()
		self:Activate()
		self.Entity:SetModel( "models/props/cs_assault/Billboard.mdl" )
		self:PhysicsInit( SOLID_VPHYSICS )      
		self:SetMoveType( MOVETYPE_VPHYSICS )
		self:SetSolid( SOLID_VPHYSICS )          
		local phys = self:GetPhysicsObject()
		if (phys:IsValid()) then
			phys:Wake()
		end
	end
	function Webscreen(ply,handler,id,encoded,decoded)
		if(SpawnGuy==nil) then return end
		if(ply==SpawnGuy) then
			datastream.StreamToClients(player.GetAll(),"CLWebscreen",decoded)
		end
	end
	datastream.Hook("Webscreen",Webscreen)
end
if CLIENT then
	require("datastream")
	ENT.RenderGroup = RENDERGROUP_BOTH
	local url = ""
	local width = 760
	local height = 390
	webscreenbrowser = vgui.Create("HTML")
	if g_3DHTML and g_3DHTML:IsValid() then g_3DHTML:Remove() end
	g_3DHTML = webscreenbrowser
	webscreenbrowser:SetEnabled(true)
	webscreenbrowser:SetPaintedManually(true)
	webscreenbrowser:SetSize(width, height)
	webscreenbrowser:SetMouseInputEnabled(false)
	webscreenbrowser:OpenURL("")
	function ENT:Initialize()
		url = ""
		datastream.StreamToServer("Webscreen",LocalPlayer():GetInfo("webscreen_url"))
	end
	local function DrawSign()
		render.SetMaterial(webscreenbrowser:GetHTMLMaterial())
		render.DrawQuad(Vector(0, 0, 0),
		Vector(width, 0, 0),
		Vector(width, height, 0),
		Vector(0, height, 0))
	end
	function ENT:Draw ()
		self:DrawModel()
		local pos = self:GetPos()+(self:GetForward()*2)+(self:GetRight()*110)+self:GetUp()*58
		local rot = Vector(-90, 90, 0)  
		local ang = self:GetAngles()
		ang:RotateAroundAxis(ang:Right(),   rot.x)  
		ang:RotateAroundAxis(ang:Up(),      rot.y)  
		ang:RotateAroundAxis(ang:Forward(), rot.z)
		cam.Start3D2D(self:GetPos()+(self:GetForward()*2)+(self:GetRight()*110)+self:GetUp()*58,ang,0.4)
		local status, err = pcall(DrawSign)
		cam.End3D2D()
		if not status then Error(err) end
	end
	function ENT:OnRemove( )
		webscreenbrowser:SetEnabled(false)
		webscreenbrowser:OpenURL("http://")
	end
	function WebscreenNewURL(temp1,temp2,temp3,decoded)
		if decoded == nil then print("Failed to make Webscreen.") return end
		if url != "" then return end
		print("Got Webscreen URL: "..decoded)
		url = decoded
		webscreenbrowser:OpenURL(url)
		g_3DHTML = webscreenbrowser
		webscreenbrowser:SetEnabled(true)
		webscreenbrowser:SetPaintedManually(true)
		webscreenbrowser:SetSize(width, height)
		webscreenbrowser:SetMouseInputEnabled(false)
	end
	datastream.Hook("CLWebscreen",WebscreenNewURL)
end